package cn.coisini.test.kafka.controller;

import cn.coisini.test.kafka.config.SpringBootKafkaConfig;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiangshaw
 * Description: Kafka生产者测试
 */
@RestController
@RequestMapping("/kafka")
@Log4j2
public class KafkaProducerController {
    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    @ResponseBody
    @PostMapping(value = "/produce", produces = "application/json")
    public String produce(@RequestBody Object obj) {

        try {
            String obj2String = JSONUtil.toJsonStr(obj);
            kafkaTemplate.send(SpringBootKafkaConfig.TOPIC_TEST, obj2String);
            return "success";
        } catch (Exception e) {
            e.getMessage();
        }
        return "success";
    }
}
